package utils

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"github.com/google/uuid"
	"math"
	"math/rand"
	"sort"
	"strconv"
	"strings"
	"time"
)

func MD5(s string) string {
	hasher := md5.New()
	hasher.Write([]byte(s))
	return hex.EncodeToString(hasher.Sum(nil))
}
func DecDate(s string) int64 {
	if s == "" {
		return 0
	}
	dd, _ := strconv.ParseInt(strings.ReplaceAll(s, "-", ""), 10, 64)
	return dd
}
func DecDateTime(s string, v string) time.Time {
	if v == "" || s == "" {
		return time.Now()
	}
	dd, _ := time.ParseInLocation(s, v, time.Local)
	return dd
}
func Decimal(num float64) float64 {
	num, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", num), 64)
	return num
}

func DecimalStr(num float64) string {
	return fmt.Sprintf("%.2f", num)
}
func IntdStr(num interface{}) string {
	return fmt.Sprintf("%d", num)
}

func DecimalIntRate(a, b int64) string {
	if b == 0 {
		return "0.00"
	}
	return DecimalStr((float64(a) / float64(b)) * 100)
}

func CkArrs(s []string, sep string) bool {
	for _, s2 := range s {
		if s2 == sep {
			return true
		}
	}
	return false
}
func DelUnqArrs(s []string, a string) []string {
	b := make(map[string]bool)
	for _, s2 := range s {
		if s2 == a {
			continue
		}
		b[s2] = true
	}
	b_arr := []string{}
	for k, _ := range b {
		b_arr = append(b_arr, k)
	}
	return b_arr
}

func UnqArrs(s []string) []string {
	b := make(map[string]bool)
	for _, s2 := range s {
		b[s2] = true
	}
	b_arr := []string{}
	for k, _ := range b {
		b_arr = append(b_arr, k)
	}
	return b_arr
}

func GetMiddleValue(arr []int) int64 {
	length := len(arr)
	if length == 0 {
		return 0
	}
	sort.Ints(arr)
	middleIndex := int(math.Floor(float64(length) / 2))

	if length%2 == 1 {
		// 如果是奇数个元素，直接返回中间的值
		return int64(arr[middleIndex])
	} else {
		// 如果是偶数个元素，返回中间两个值的平均值
		return int64(arr[middleIndex])
	}
}

func RandomString(length int) string {
	chars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	result := ""
	for i := 0; i < length; i++ {
		result += string(chars[rand.Intn(len(chars))])
	}
	return result
}
func RandFloat() float64 {
	return rand.Float64()
}

func GetUUid() string {
	u, err := uuid.NewRandom()
	if err != nil {
		return GetUUid()
	}
	return u.String()
}

func RandInt64(min, max int64) int {
	if max == 0 {
		return 0
	}
	tt := rand.Int63n(max)
	return int(tt)
}

// 随机返回n个不重复元素（原数组不变）
func RandomSample[T any](arr []T, n int) []T {
	if n <= 0 || len(arr) == 0 {
		return []T{}
	}

	rand.Seed(time.Now().UnixNano())
	shuffled := make([]T, len(arr))
	copy(shuffled, arr)
	rand.Shuffle(len(shuffled), func(i, j int) {
		shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
	})

	if n > len(arr) {
		n = len(arr)
	}
	return shuffled[:n]
}

func RandomSampleInt(max int, n int) []int {
	if max == 0 {
		return nil
	}
	arr := []int{}
	for i := 0; i < max; i++ {
		arr = append(arr, i)
	}

	rand.Seed(time.Now().UnixNano())
	shuffled := make([]int, max)
	copy(shuffled, arr)
	rand.Shuffle(len(shuffled), func(i, j int) {
		shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
	})

	if n > len(arr) {
		n = len(arr)
	}
	return shuffled[:n]
}

func Rate(a, b int64) float64 {
	if a == 0 || b == 0 {
		return 0.00
	}
	t := Decimal((float64(a) / float64(b)) * 100)
	return t
}
func RateF(a, b float64) float64 {
	if a == 0 || b == 0 {
		return 0.00
	}
	t := Decimal((float64(a) / float64(b)) * 100)
	return t
}
